home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 300_01 / contour.c < prev    next >
C/C++ Source or Header  |  1989-12-28  |  24KB  |  457 lines

  1. /*   HEADER:   CUG300;
  2.       TITLE:   Contour ploting demonstration;
  3.        DATE:   4/20/1989;
  4. DESCRIPTION:   "Plot contours using mat.lib functions";
  5.     VERSION:   2.04;
  6.    FILENAME:   CONTOUR.C;
  7.    SEE-ALSO:   MAT_V2D.H;
  8. */
  9.  
  10. /*===================================================================*/
  11. /* ROUTINES TO GENERATE CADD CONTOUR LINES FROM INPUT GRID VALUES    */
  12. /*===================================================================*/
  13. /*      0,1       1,1                                                */
  14. /*   m4.-----------.m3            ^ 0,jub                            */
  15. /*     | .       . |              |                                  */
  16. /*     |   .   .   |              |                                  */
  17. /*     |     .m0   |              |                                  */
  18. /*     |    .  .   |              |                                  */
  19. /*     |  .      . |              |                                  */
  20. /*   m1|___________|m2           0,0 --------------> iub,0           */
  21. /*     0,0        1,0                                                */
  22. /*                                                                   */
  23. /*   grid square layout            coordinate system layout          */
  24. /*   -------------------           ------------------------          */
  25. /*===================================================================*/
  26.  
  27. #include <stdio.h>
  28. #include "mat_v2d.h"
  29. #include <math.h>
  30. #include <stdlib.h>
  31. #include <conio.h>
  32.  
  33.  
  34. #define  FALSE       0
  35. #define  TRUE        1
  36. #define  NO_ROWS     24
  37. #define  NO_COLS     72
  38.  
  39. #define  xchk(x)     if(x<0)puts("x<0");if(x>NO_COLS)puts("x>NO_COLS")
  40. #define  ychk(y)     if(y<0)puts("y<0");if(y>NO_COLS)puts("y>NO_COLS")
  41.  
  42. void  box          (float x1,float y1,float x2,float y2 );
  43. void  dda          (float x1,float y1,float x2,float y2,char symbol);
  44. void  display      (void);
  45. void  erase        (void);
  46. void  GridSelect   (int iub, int jub, int nc, struct fmat *x,
  47.                     struct fmat *y,struct fmat *d, struct fmat *z);
  48. void ContourSelect (int i,int j, int nc,struct fmat *x,
  49.                           struct fmat *y,struct fmat *d,struct fmat *z,
  50.                           int iub, int jub);
  51. void drawit        (float x1, float x2, float y1, float y2,
  52.                     struct fmat *x, struct fmat *y,int iub,int jub );
  53. void set_graphics  (void);
  54. void file_output   (void);
  55.  
  56.  
  57. char  frame[NO_COLS][NO_ROWS];
  58.  
  59.  
  60. /*============================================================================*/
  61.  
  62. main()
  63.  
  64. /*============================================================================*/
  65.  
  66. {
  67. int i,i1,j,j1,k,iub,jub,nc,prmerr,num_rows,num_cols,token_size;
  68. struct   fmat *x,*y,*d,*z;
  69. struct   tmat *input_data;
  70. char     filename[20];
  71.  
  72. /*       Input arrays and indexes                                             */
  73.  
  74.     printf("Enter contour data filename => ");
  75.    scanf("%s",filename);
  76.    mtcnt(filename,&num_rows,&num_cols,&token_size);    /* Get input file size */
  77.    tdim(input_data,num_rows,num_cols,token_size);      /* Dimension input     */
  78.    mtget(filename,input_data);                         /* array               */
  79.  
  80.     iub = no_toks(input_data,0);                        /* Get indices from    */
  81.     jub = no_toks(input_data,1);                        /* the number of tokens*/
  82.     nc  = no_toks(input_data,2);                        /* stored in the       */
  83.                                                        /* appropriate input   */
  84.    fdim(d,iub,jub+1);                                  /* data line header    */
  85.    fdim(x,iub,DCLVCT);
  86.    fdim(y,jub,DCLVCT);                                 /* Dimension needed    */
  87.    fdim(z,nc,DCLVCT);                                  /* arrays              */
  88.  
  89.     for(i=0; i<iub; i++) {                              /* Store x grid loca-  */
  90.       fck(x,i,VCT);                                    /* tion.               */
  91.         f(x,i,VCT) = tf(input_data,0,i,iub);
  92.    }
  93.     for(j=0; j<jub; j++) {                              /* Store y grid loca-  */
  94.       fck(y,j,VCT);                                    /* tions               */
  95.         f(y,j,VCT) = tf(input_data,1,j,jub);
  96.    }
  97.    for(i=0; i<nc; i++) {                               /* Store contour       */
  98.       fck(z,i,VCT);                                    /* levels to be        */
  99.       f(z,i,VCT) = tf(input_data,2,i,nc);              /* plotted             */
  100.    }
  101.    for (j=3,k=jub-1; j<num_rows; j++,k--)  {
  102.       for (i=0; i<iub; i++) {                          /* Store f(x,y) for    */
  103.             fck(d,i,k);                                   /* each grid line      */
  104.             f(d,i,k) = tf(input_data,j,i,iub);            /* intersection        */
  105.       }
  106.    }
  107.    iub--;jub--;
  108.  
  109. /*       Check input parameters for validity                                  */
  110.  
  111.    prmerr = FALSE;                                     /* Test for no grid    */
  112.     if (iub<=0 || jub<=0)                               /* intersections.      */
  113.       prmerr = TRUE;                                   /*                     */
  114.    if (nc <=0)                                         /* No contours         */
  115.       prmerr = TRUE;                                   /* requested           */
  116.    for (k = 1; k <= (nc-1); k++)                       /*                     */
  117.    {                                                   /*                     */
  118.       if (f(z,k,VCT) <= f(z,k-1,VCT) )                 /* Duplicate contours  */
  119.          prmerr = TRUE;                                /*                     */
  120.    }                                                   /*                     */
  121.    if (prmerr)                                         /*                     */
  122.    {                                                   /*                     */
  123.       printf ("Error in input parameters");            /* Message if any      */
  124.       return;                                          /* errors found        */
  125.    }                                                   /*                     */
  126.    set_graphics();                                     /* Initialize graphic  */
  127.                                                        /* frame buffer        */
  128.    GridSelect (iub, jub, nc, x, y, d, z );             /* Plot contours       */
  129.                                                        /*                     */
  130.    display();                                          /* Display all         */
  131.                                                        /* contours plotted    */
  132.    file_output();                                      /*                     */
  133.                                                        /* Place contour in    */
  134.                                                        /* an ASCII file       */
  135. }                                                      /*                     */
  136.                                                                               
  137.  
  138. /*============================================================================*/
  139.  
  140. void GridSelect  (   int iub, int jub, int nc, struct fmat *x,
  141.                      struct fmat *y,struct fmat *d, struct fmat *z )
  142.  
  143. /*============================================================================*/
  144. {
  145. int i, j;
  146.  
  147.  
  148. /*    Scan the input array top down & left to right                           */
  149.  
  150.  
  151.  
  152.    for  (j = (jub-1); j >= 0; j--)
  153.    {
  154.       for (i = 0; i <= (iub-1); i++)
  155.       {
  156.         ContourSelect (i,j,nc,x,y,d,z,iub,jub);          /* Select contour  to  */
  157.       }                                                /* plot                */
  158.    }
  159. }
  160.  
  161. /*============================================================================*/
  162.  
  163. void ContourSelect ( int i,int j, int nc,struct fmat *x,
  164.                      struct fmat *y, struct fmat *d,
  165.                      struct fmat *z, int iub, int jub )
  166.  
  167. /*============================================================================*/
  168.  
  169. {
  170.  
  171. /*   Declare & initialize variables                                           */
  172.  
  173. float     h [5],    xh [5],     yh[5];
  174.  
  175. int      ish[5];
  176. int    caseval,k,i1,j1,m1, m2, m3;
  177. int            m;
  178. static int  im[4] =   { 0,